- 해당 자료는 전북대학교 통계학과 고급데이터시각화 수업을 바탕으로 작성하였습니다.
- 자료출처
import
import matplotlib.pyplot as plt
import numpy as np
boxplot
예제 1
- 평균은 좋은 추정값인가?
- 전북고등학교에서 통계학을 수업하는 A선생님과 B선생님의 있다. A선생님에게서 수업을 들을 학생들의 평균은 79.1이고 B선생님에게서 수업을 들은 학생들의 평균은 78.3이다.
y1= [75 ,75 ,76 ,76 ,77 ,77 ,79 ,79 ,79 ,98 ] # A선생님에게 통계학을 배운 학생의 점수들
y2= [76 ,76 ,77 ,77 ,78 ,78 ,80 ,80 ,80 ,81 ] # B선생님에게 통계학을 배운 학생의 점수들
- 의사결정 : A선생님에게 배운 학생들의 실력이 평균적으로 더 좋을 것이다.
matplotlib
plt.boxplot([y1,y2])
plt.legend(["A" ,"B" ])
<matplotlib.legend.Legend at 0x1f0c61c5fd0>
plotly
import plotly.express as px
import pandas as pd
y1= [75 ,75 ,76 ,76 ,77 ,77 ,79 ,79 ,79 ,98 ] # A선생님에게 통계학을 배운 학생의 점수들
y2= [76 ,76 ,77 ,77 ,78 ,78 ,80 ,80 ,80 ,81 ] # B선생님에게 통계학을 배운 학생의 점수들
df= pd.DataFrame({'score' :y1+ y2,'class' :['A' ]* len (y1) + ['B' ]* len (y2)})
df.head()
score
class
0
75
A
1
75
A
2
76
A
3
76
A
4
77
A
px.box(df,x= "class" , y= "score" )
histogram
- 궁금한 것 : 그래서 A반과 B반 중 어떤 반이 공부를 더 잘하냐?
보통 이러한 질문은 중심경향값 중 하나를 골라서 비교하면 되었다.
- boxplot으로 전체 분포를 파악해도 어떠한 반이 더 공부를 잘한다는 기준을 잡는것이 애매함.
- 특수한 경우(정규분포)에는 평균으로 누가 더 공부를 잘하는지 확인할 수있다.
np.random.seed(202150256 )
y1 = np.random.randn(10000 )
y2 = np.random.randn(10000 ) + 0.5
- 결론 두 집단 모두 정규분포를 가정했을 때 우리의 궁금증은 해결이 가능하다.
- 정규분포 확인 방법 \(\to\) 히스토그램을 그려보아서 종모양이 나오는지 살펴보자.
matplotlib
plt.hist([y1,y2],bins= 50 );
plt.legend(["A" ,"B" ])
<matplotlib.legend.Legend at 0x1f0cc1a9790>
seaborn
df= pd.DataFrame({'score' :np.concatenate([y1,y2]), 'class' :['A' ]* len (y1)+ ['B' ]* len (y2)})
df.head()
score
class
0
0.944267
A
1
-0.393919
A
2
-0.854069
A
3
-0.732953
A
4
1.329115
A
sns.histplot(df,x= "score" ,hue= "class" )
<AxesSubplot:xlabel='score', ylabel='Count'>
plotnine
ggplot(df) + geom_histogram(aes(x= "score" ,fill= "class" ),position= "identity" ,alpha= 0.5 )
C:\Users\rkdcj\anaconda3\lib\site-packages\plotnine\stats\stat_bin.py:95: PlotnineWarning: 'stat_bin()' using 'bins = 79'. Pick better value with 'binwidth'.
plotly
import plotly.figure_factory as ff
hist_data = [y1,y2]
group_labels = ["A" , "B" ]
ff.create_distplot(hist_data, group_labels,bin_size= 0.2 ,show_rug= False )